package org.jeecg.modules.demo.staffmanagement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.demo.staffmanagement.entity.SaleDetail;
import org.jeecg.modules.demo.staffmanagement.entity.ServiceDetail;
import org.jeecg.modules.demo.staffmanagement.entity.StaffManagement;
import org.jeecg.modules.demo.staffmanagement.entity.StaffShareDetail;
import org.jeecg.modules.demo.staffmanagement.mapper.StaffManagementMapper;
import org.jeecg.modules.demo.staffmanagement.service.IStaffManagementService;
import org.jeecg.modules.util.DateUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description: 员工管理
 * @Author: jeecg-boot
 * @Date: 2021-12-31
 * @Version: V1.0
 */
@Service
@Slf4j
public class StaffManagementServiceImpl extends ServiceImpl<StaffManagementMapper, StaffManagement> implements IStaffManagementService {
    @Resource
    private StaffManagementMapper staffManagementMapper;

    @Override
    public Map<String, Object> getStaffAward(String id, String startTime, String endTime) {
        return staffManagementMapper.getStaffAward(id, startTime, endTime);
    }

    @Override
    public IPage<StaffManagement> getDataList(Page<StaffManagement> page, String startTime, String endTime, String searchSql, String orderby, String storeId) {
        return staffManagementMapper.getDataList(page, startTime, endTime, searchSql, orderby, storeId);
    }

    @Scheduled(cron = "0 0 0 1 * ?")
    public void sendSalary() {
        log.info("月初下发工资");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Calendar now = Calendar.getInstance();
        now.setTime(new Date());
        String nowTimeStr = sdf.format(now.getTime());
        // now是上个月1月1号
        now.add(Calendar.MONTH, -1);
        // 获取上个月最后一天
        int lastDay = now.getActualMaximum(Calendar.DAY_OF_MONTH);

        List<StaffManagement> list = staffManagementMapper.selectList(new QueryWrapper<>());
        for (StaffManagement sm : list) {
            Date entryTime = sm.getEntryTime();
            String entryTimeStr = sdf.format(entryTime);
            if (entryTimeStr.equals(nowTimeStr)) {
                int compare = DateUtils.compareDay(new Date(), entryTime) - 1;
                double salary = sm.getTotalSalary() + sm.getBasePay() * compare / lastDay;
                sm.setTotalSalary(salary);
                log.info("[员工id：{}]，发放{}天工资{}元。（本月共{}天）", sm.getId(), compare, lastDay, salary);
            } else {
                sm.setTotalSalary(sm.getTotalSalary() + sm.getBasePay());
                log.info("[员工id：{}]，发放整月工资{}元。", sm.getId(), sm.getBasePay());
            }
            staffManagementMapper.updateById(sm);
        }
    }

    @Override
    public void updateTotal(String id, Double totalSell, Double sellMoney, Double serveMoney, Double totalShare) {
        staffManagementMapper.updateTotal(id, totalSell, sellMoney, serveMoney, totalShare);
    }

    @Override
    public void updateTotalSell(String startTime, String endTime) {
        staffManagementMapper.updateTotalSell(startTime, endTime);
    }

    @Override
    public void updateSellMoney(String startTime, String endTime) {
        staffManagementMapper.updateSellMoney(startTime, endTime);
    }

    @Override
    public void updateServeMoney(String startTime, String endTime) {
        staffManagementMapper.updateServeMoney(startTime, endTime);
    }

    @Override
    public void updateTotalDeduction() {
        staffManagementMapper.updateTotalDeduction();
    }

    @Override
    public void updateTotalShare(String startTime, String endTime) {
        staffManagementMapper.updateTotalShare(startTime, endTime);
    }

    @Override
    public void updateTotalSalary(String startTime, String endTime) {
        staffManagementMapper.updateTotalSalary(startTime, endTime);
    }

    @Override
    public void updateTotalSalary2(Long days, String startTime, String endTime) {
        staffManagementMapper.updateTotalSalary2(days, startTime, endTime);
    }

    @Override
    public List<SaleDetail> selectSaleDetail(String orderCode, String memberName, String staffName, String staffTel, String startTime, String endTime, String storeId) {
        return staffManagementMapper.selectSaleDetail(orderCode, memberName, staffName, staffTel, startTime, endTime, storeId);
    }

    @Override
    public List<ServiceDetail> selectServiceDetail(String orderCode, String memberName, String staffName, String staffTel, String startTime, String endTime, String storeId) {
        return staffManagementMapper.selectServiceDetail(orderCode, memberName, staffName, staffTel, startTime, endTime, storeId);
    }

    @Override
    public List<StaffShareDetail> selectStaffShareDetail(String orderCode, String memberName, String staffName, String staffTel, String startTime, String endTime, String storeId) {
        return staffManagementMapper.selectStaffShareDetail(orderCode, memberName, staffName, staffTel, startTime, endTime, storeId);
    }

    @Override
    public Double getCommoditySell(String staffId, String startTime, String endTime) {
        return staffManagementMapper.getCommoditySell(staffId, startTime, endTime);
    }

    @Override
    public Double getProjectSell(String staffId, String startTime, String endTime) {
        return staffManagementMapper.getProjectSell(staffId, startTime, endTime);
    }

    @Override
    public Double getCommodityMoney(String staffId, String startTime, String endTime) {
        return staffManagementMapper.getCommodityMoney(staffId, startTime, endTime);
    }

    @Override
    public Double getProjectMoney(String staffId, String startTime, String endTime) {
        return staffManagementMapper.getProjectMoney(staffId, startTime, endTime);
    }

    @Override
    public Double getServeMoney(String staffId, String startTime, String endTime) {
        return staffManagementMapper.getServeMoney(staffId, startTime, endTime);
    }

    public Double getTotalShare(String staffId, String startTime, String endTime) {
        return staffManagementMapper.getTotalShare(staffId, startTime, endTime);
    }
}
